package com.zhongke.DKYCboot.controller;

import com.zhongke.DKYCboot.bean.BaseCar;
import com.zhongke.DKYCboot.bean.BaseDriver;
import com.zhongke.DKYCboot.service.BaseCarService;
import com.zhongke.DKYCboot.util.FilePath;
import com.zhongke.DKYCboot.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@CrossOrigin
@RequestMapping("/Base_Carcontroller")
@ResponseBody
public class BaseCarcontroller {
    @Autowired
//    注入service接口
    private BaseCarService base_carService;
   //图片服务器地址
    @Value("${imageServerUrl}")
    private String imageServerUrl;
    //图片保存根路径
    @Value("${imgBasePath1}")
    private String imgBasePath1;
    //图片字段
    @Value("${carPic}")
    private String car_Pic;
    /**
     * 上传车辆图片，生成路径
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/uploadCarPic" ,method = RequestMethod.POST)
    public Msg uploadServiceOrder(MultipartFile file) throws IOException {
        String path = imgBasePath1 + car_Pic;
        String filePath = FilePath.upload(file, path);
        String proMainImage = car_Pic + filePath;
        return Msg.success(proMainImage);
    }



    @RequestMapping(value = "/getall" ,method = RequestMethod.POST)
    //查询所有车辆信息
    public Msg getall() {
        //调用service层的查询方法
        return base_carService.selectCar();

    }

    @RequestMapping(value = "/getCarByBrand" ,method = RequestMethod.POST)

    /**
     * 根据车辆品牌查询所有车辆
     */
    public Msg getCarByBrand(@RequestBody BaseCar baseCar) {
        //调用service层的查询方

        return   base_carService.selectCarByBrand(baseCar);

    }

    /**
     * 模糊查询
     * @param base_car
     * @return
     */
    @RequestMapping(value = "/selectCarByCar" ,method = RequestMethod.POST)
    public Msg selectCarByplatenum(@RequestBody BaseCar base_car) {
//        调用Service层的方法


        return base_carService.selectCarByCar(base_car);
    }


    /**
     * 新增车辆
     * @param base_car
     * @return
     */

    @RequestMapping(value = "/saveAdd",method = RequestMethod.POST)
    public Msg saveUser(@RequestBody BaseCar base_car) {
        Msg aaa = base_carService.addBaseCar(base_car);

        return aaa;
    }

    /**
     * 批量删除
     * @param id 根据Id进行批量删除
     * @return
     */
    @RequestMapping(value = "/saveUp" ,method = RequestMethod.POST)
    public Msg deleteVillages(@RequestBody int[] id){
        if (id != null && id.length != 0){
            for(int i=0;i<id.length;i++){
                base_carService.updateBase(id[i]);
            }
            Msg.success("ok");
            Msg.success("删除成功");
        }else{
            Msg.success("error");
            Msg.success("删除失败");
        }
        return  Msg.success("删除成功");
    }
    /*
    *
    *修改车辆信息
    *
    * */
    @RequestMapping(value = "updateBaseCar" ,method = RequestMethod.POST)
    public  Msg updateBaseCar(@RequestBody BaseCar baseCar){

        return base_carService.updateBaseCar(baseCar);
    }

}


